package com.kuang.other;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2021/5/28 11:04
 * Description: 练习用的类，与多线程无关
 */
public class TestDemo {
    public static final int HANDLETOKENNUM = 1000;
    /**
     * 将一个list拆分成多个list,每个list至多装1000条
     * @param list 目标list
     * @param n 拆分成的个数
     * @return 返回的list
     */
    public static List<List<String>> getList(List<String> list, int n) {
        List<List<String>> mList = new ArrayList<>();
        if (list==null || list.size()==0) {
            return mList;
        }
        if (n <= 0) {
            throw new RuntimeException("n must bigger than 0");
        }
        //装配成几个list
        boolean b = list.size() % n == 0;
        int x =  b ? list.size() / n : list.size() / n + 1;
        //等待装配的list
        List<String> iList = null;
        for (int i = 0; i < x; i++) {
            iList = new ArrayList<>(1000);
            int nextStart = i * 1000;
            int nextEnd;
            if (i == x - 1) {
                nextEnd = i * 1000 + (b ? 1000 : list.size() % n);
            }else {
                nextEnd = (i + 1) * 1000;
            }
//            for (int j = nextStart; j < nextEnd; j++) {
//                iList.add(list.get(j));
//            }
            iList.addAll(list.subList(nextStart, nextEnd));
            mList.add(iList);
        }
        return mList;
    }

    @Test
    public void test01() {
        List<String> list = new ArrayList<>(2080);
        for (int i = 0; i < 2080; i++) {
            list.add(String.valueOf(i));
        }
        List<List<String>> list1 = getList(list, HANDLETOKENNUM);
        for (List<String> strings : list1) {
            System.out.println(strings);
        }
    }
}
